<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${table.packageNames.mapper}.${table.mapperName}">

<#if isGenerateBaseResultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${table.packageNames.entity}.${table.entityName}">
        <#list table.fields as field>
            <#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.column}" property="${field.propertyName}" />
            </#if>
        </#list>
<#list table.fields as field>
    <#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.column}" property="${field.propertyName}" />
    </#if>
</#list>
    </resultMap>

</#if>
<#if isGenerateBaseColumnList>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ${table.fieldsSeparation}
    </sql>
</#if>
<#if isGenerateBaseCRUD>
    <!--批量添加-->
    <insert id="saveBatch" keyColumn="${table.primaryKey!}" useGeneratedKeys="true"  keyProperty="${table.primaryKeyName!}">
        INSERT INTO `${table.name}` (${table.fieldsSeparation})
        VALUES
        <foreach collection="list" item="item" separator=",">
                (${table.fieldsSaveBach})
        </foreach>
    </insert>
    <!--添加一条数据-->
    <insert id="save"   keyColumn="${table.primaryKey!}" useGeneratedKeys="true"  keyProperty="${table.primaryKeyName!}">
        insert into `${table.name}`(${table.fieldsSeparation})
        values(${table.fieldsSaveItemBach})
    </insert>
    <!--添加对象中不为空的数据-->
    <insert id="saveNotEmpty"  keyColumn="${table.primaryKey!}" useGeneratedKeys="true"  keyProperty="${table.primaryKeyName!}">
        insert into `${table.name}`
        <trim prefix="(" suffix=")" suffixOverrides=",">
         <#list table.fields as field>
             <if test="${field.propertyName} != null <#if field.propertyType == 'String'>and ${field.propertyName} != ''</#if>">${field.column},</if>
         </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list table.fields as field>
             <if test="${field.propertyName} != null <#if field.propertyType == 'String'>and ${field.propertyName} != ''</#if>">${field.xmlEntryParameters},</if>
            </#list>
        </trim>
    </insert>
    <!--根据id修改-->
    <update id="updateById">
        update `${table.name}` set
        ${table.fieldUpdateBach}
        where `${table.primaryKey}`=${table.capitalPrimaryKeyName}
    </update>
    <!--根据id修改 对象中不为空的数据-->
    <update id="updateNotEmptyById">
        update `${table.name}`
        <set>
    <#list table.fields as field>
            <if test="${field.propertyName} != null <#if field.propertyType == 'String'>and ${field.propertyName} != ''</#if>">
                ${field.column}=${field.xmlEntryParameters},
            </if>
    </#list>
        </set>
        where `${table.primaryKey}`=${table.capitalPrimaryKeyName}
    </update>
    <!--根据id删除-->
    <delete id="deleteById">
        delete from `${table.name}`
        where `${table.primaryKey}`=${table.capitalPrimaryKeyName}
    </delete>
    <!--根据 实体不为空的数据查询-->
    <select id="findListByEntity" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `${table.name}`
        <where>
                <#list table.fields as field>
            <if test="${field.propertyName} != null <#if field.propertyType == 'String'>and ${field.propertyName} != ''</#if>">and ${field.column}=${field.xmlEntryParameters}</if>
                </#list>
        </where>
    </select>
    <!--根据 实体不为空的数据查询-->
    <select id="get${table.entityName}ByEntity" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `${table.name}`
        <where>
                <#list table.fields as field>
                    <if test="${field.propertyName} != null <#if field.propertyType == 'String'>and ${field.propertyName} != ''</#if>">and ${field.column}=${field.xmlEntryParameters}</if>
                </#list>
        </where>
        limit 1
    </select>
    <!--根据id查询对象-->
    <select id="get${table.entityName}ById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `${table.name}`
        WHERE `${table.primaryKey}`=${table.capitalPrimaryKeyName}
    </select>
</#if>
</mapper>
